/*************************************************************************
	> File Name: httcp.h
	> Author: xuwenlong
	> Mail: myxuan475@126.com
	> Created Time: 2018年01月17日 星期三 11时29分28秒
 ************************************************************************/
#ifndef FOUNDATION_DOWNLINK_TCP_H
#define FOUNDATION_DOWNLINK_TCP_H

#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <utils/lock.h>
#include <vector>
#include <utils/logger.h>
#include "downlink_connect.h"

namespace downlink {
class SocketConnect : public DownlinkConnect {
public:
	SocketConnect(const dbms::DeviceChannel_t &dc);
	virtual ~SocketConnect();

private:
	virtual void ClearWriteBuffer();
	virtual void ClearReadBuffer();

	/*
	 * 发送和接收数据
	 */
	int32_t Open();
	void Close();
	int32_t Available(int &timeout);

protected:
	virtual const std::string Name() const override;
	virtual bool ReConnect() override;
	virtual bool Send(const uint8_t cmd[], int32_t len) override;
	virtual int32_t Recv(
		uint8_t cmd[], int32_t max, int32_t timeout = -1) override;
	virtual void CloseConnect();
	virtual int32_t Handle();
	bool IsAvailable();

private:
	int socketFd_ = -1;
	dbms::Socket_t socket_;
};
} // namespace downlink
#endif // FOUNDATION_DOWNLINK_TCP_H
